﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.Primitive;
using Happy.Domain;

namespace Happy.Application.Domain
{
    /// <summary>
    /// 应用服务基类。
    /// </summary>
    public abstract class ApplicationService : ServiceBase
    {
    }

    /// <summary>
    /// 应用服务基类。
    /// </summary>
    public abstract class ApplicationService<TAggregate, TId, TUnitOfWork, TRepository> : ServiceBase
        where TAggregate : IAggregateRoot<TId>
        where TUnitOfWork : IUnitOfWork
        where TRepository : IRepository<TAggregate, TId>
    {
        /// <summary>
        /// 提交变化。
        /// </summary>
        protected virtual void SaveChanges()
        {
            this.UnitOfWork.Flush();
        }

        /// <summary>
        /// 仓储。
        /// </summary>
        protected TRepository Repository
        {
            get
            {
                return this.Service<TRepository>();
            }
        }

        /// <summary>
        /// 工作单元。
        /// </summary>
        protected TUnitOfWork UnitOfWork
        {
            get
            {
                return this.Service<TUnitOfWork>();
            }
        }

        /// <summary>
        /// 扮演指定的角色。
        /// </summary>
        protected TRole PalyRole<TRole>(TAggregate actor)
            where TRole : IRole<TAggregate, TId>
        {
            var role = this.Service<TRole>();
            role.SetActor(actor);

            return role;
        }
    }

    /// <summary>
    /// 应用服务基类。
    /// </summary>
    public abstract class ApplicationService<TAggregate, TId, TUnitOfWork> : ApplicationService<TAggregate, TId, TUnitOfWork, IRepository<TAggregate, TId>>
        where TAggregate : IAggregateRoot<TId>
        where TUnitOfWork : IUnitOfWork
    {
    }
}
